import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public int removeElement(int[] nums, int val) {
        int k = 0;
        for (int x : nums) {
            if (x != val){
                nums[k++] = x;
            }
        }
        return k;
    }

    public int removeDuplicates(int[] nums) {
        int k = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != nums[k]){
                nums[++k] = nums[i];
            }
        }
        return k+1;
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m -1;
        int p2 = n -1;
        int tail = m + n - 1;
        int cur;
        while (p1 >= 0 || p2 >= 0){
            if (p1 == -1){
                cur = nums2[p2--];
            } else if (p2 == -1){
                cur = nums1[p1--];
            } else if (nums1[p1] > nums2[p2]) {
                cur = nums1[p1--];
            } else {
                cur = nums2[p2--];
            }
            nums1[tail--] = cur;
        }
    }

    public void merge1(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i < n; i++) {
            nums1[m+i] = nums2[i];
        }
        Arrays.sort(nums1);
    }

    /*public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        ret.add(list);
        for (int i = 1; i < numRows; i++) {
            List<Integer> curRow = new ArrayList<>();
            curRow.add(1);
            List<Integer> preRow = ret.get(i-1);
            for (int j = 1; j < i; j++) {
                int val = preRow.get(j) + preRow.get(j-1);
                curRow.add(val);
            }
            curRow.add(1);
            ret.add(curRow);
        }
        return ret;
    }*/

    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        ret.add(list);
        for (int i = 1; i < numRows; i++) {
            List<Integer> curRow = new ArrayList<>();
            curRow.add(1);
            List<Integer> preRow = ret.get(i-1);
            for (int j = 1; j < i; j++) {
                int val = preRow.get(j) + preRow.get(j-1);
                curRow.add(val);
            }
            curRow.add(1);
            ret.add(curRow);
        }
        return ret;
    }

}